package cn.stronglink.asset.module.assetmanage.instock.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import cn.stronglink.asset.model.Preset;
import cn.stronglink.asset.model.PresetDetail;
import cn.stronglink.asset.module.assetmanage.instock.mapper.PresetDetailMapper;
import cn.stronglink.asset.module.assetmanage.instock.mapper.PresetMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class PresetService {

	@Autowired
	private PresetMapper presetMapper;
	
	@Autowired
	private PresetDetailMapper presetDetailMapper;
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<Preset> queryList(Page<Preset> page, Map<String, Object> params) {
		page.setRecords(presetMapper.queryList(page,params));
		return page;	
	}
	
	public void removePreset(Long id){
		presetMapper.removePreset(id);	
		presetDetailMapper.removePresetDetailByPresetId(id);
	}

	public void insertPreset(Preset preset) {
		if (preset.getId() == null) {
			preset.setId(IdWorker.getId());
			preset.setStatus(1);
			preset.setCreateBy(WebUtil.getCurrentUser());
			preset.setCreateTime(new Date());
			presetMapper.insertPreset(preset);		
		}else{
			preset.setUpdateTime(new Date());
			preset.setUpdateBy(WebUtil.getCurrentUser());
			presetMapper.updatePreset(preset);
		}	
		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Preset qryById(Long id) {
		return presetMapper.qryById(id);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public int qryPresetCount() {
		return presetMapper.qryPresetCount();
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Preset> qryCheckList(Long userId) {
		List<Preset> list = presetMapper.qryCheckList(userId);
		if(list!=null&&list.size()>0){
			for(Preset preset:list){
				List<PresetDetail> dList = presetDetailMapper.queryDetailList(preset.getId());
				preset.setCount(0);
				if(dList!=null&&dList.size()>0){
					preset.setDetailList(dList);
					preset.setCount(dList.size());
				}				
			}
		}
		return list;
	}



}
